package com.ccys.cms.service.userManagement.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.cms.service.userManagement.DoctorSpecialistService;
import com.ccys.common.util.Kit;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.userManagement.DoctorSpecialistDto;
import com.ccys.db.entity.TblDoctorSpecialist;
import com.ccys.db.mapper.TblDoctorSpecialistMapper;
import com.ccys.db.page.PageDto;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.service.BaseTblAuditTrailWebService;
import com.ccys.db.vo.masterData.DoctorSpecialistPageQueryVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @program: WhiteCoat
 * @description: 外部专科医生接口实现类
 * @author: ZhangJingCan
 * @create: 2023-12-20 11:20
 **/

@Service
public class DoctorSpecialistServiceImpl extends BaseServiceImpl<TblDoctorSpecialist> implements DoctorSpecialistService {

    @Resource
    private TblDoctorSpecialistMapper tblDoctorSpecialistMapper;

    /**
     * 用于保存修改状态日志
     */
    @Autowired(required = false)
    private BaseTblAuditTrailWebService baseTblAuditTrailWebService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult saveDoctorSpecialist(DoctorSpecialistDto dto) {
        //校验字段值
        Kit.allParamValid(dto, "specialisationId", "name", "mobile", "companyId");
        //新增/修改 - 并判断是否成功返回
        return ResponseResult.update(this.insertOrUpdate(dto));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteDoctorSpecialist(DoctorSpecialistDto dto) {
        //判断
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备实体类并赋值
        TblDoctorSpecialist tblDoctorSpecialist = new TblDoctorSpecialist();
        tblDoctorSpecialist.setId(dto.getId());
        tblDoctorSpecialist.setDelFlag(1);
        //判断是否修改成功 - 返回不同状态
        if (tblDoctorSpecialistMapper.updateByPrimaryKeySelective(tblDoctorSpecialist) > 0) {
            return ResponseResult.success("Delete Successful!");
        }
        return ResponseResult.error("Delete Failed!");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult updateDoctorSpecialistStatus(DoctorSpecialistDto dto) {
        //校验
        if (ObjectUtil.isEmpty(dto.getId()) || ObjectUtil.isEmpty(dto.getStatus())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备数据修改
        TblDoctorSpecialist tblDoctorSpecialist = new TblDoctorSpecialist();
        tblDoctorSpecialist.setId(dto.getId());
        tblDoctorSpecialist.setStatus(dto.getStatus());
        if (tblDoctorSpecialistMapper.updateByPrimaryKeySelective(tblDoctorSpecialist) > 0) {
            //保存状态修改日志
            baseTblAuditTrailWebService.saveStatusLog(dto, dto.getStatus());
            return ResponseResult.success("Modified successfully!");
        }
        return ResponseResult.error("Modification failed!");
    }

    @Override
    public ResponseResult<PageInfo<DoctorSpecialistPageQueryVo>, Object> findPageDoctorSpecialist(DoctorSpecialistDto dto) {
        dto.setStatus(Optional.ofNullable(dto.getStatus()).filter(s -> s > -1 && s < 2).orElse(null));
        //开启分页
        PageDto.startPage();
        List<DoctorSpecialistPageQueryVo> list = tblDoctorSpecialistMapper.findPageDoctorSpecialist(dto);
        PageInfo<DoctorSpecialistPageQueryVo> info = new PageInfo<>(list);
        return ResponseResult.success(info);
    }
}
